<?php
$GLOBALS[jumpurl]=array();
$cookie="";
function curl_file_get_contents($durl, &$cookie="",$Referer="",$header=1,$proxy="")
{
    static $oldurl;
    $ch = curl_init();
    $Referer=($Referer=="")?"http://".parse_url($durl)[host]:$Referer;
    curl_setopt($ch, CURLOPT_URL, $durl);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
    if ($proxy!="") {
        curl_setopt($ch,CURLOPT_PROXY, $proxy);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if($Referer!=""&&$Referer!="http://"){
        curl_setopt($ch, CURLOPT_REFERER, $Referer);
    }
    if($cookie!=""){
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, $header);
    $r = curl_exec($ch);
    curl_close($ch);

    if (preg_match("/Location:\s*([^\r\n]+)/isx", $r, $nn)) {
        $GLOBALS['jumpurl']="";
        if($durl!=trim($nn[1])&&$oldurl!=$durl) {
            $oldurl=$durl;
            $durl = trim($nn[1]);
            $GLOBALS['jumpurl']=$durl;
            $r = curl_file_get_contents($durl, $cookie, $Referer,$header,$proxy);
			
        }
    }

    if (preg_match("/self\.location=\"([^<>]*)\"\;/Uisx", $r, $nn)) {
        $GLOBALS['jumpurl']="";
        if($durl!=trim($nn[1])&&$oldurl!=$durl) {
            $oldurl=$durl;
            $durl = quanurl(trim($nn[1]), "http://".parse_url($durl)[host]."/");
            $r = curl_file_get_contents($durl, $cookie, $Referer,1);
            $GLOBALS['jumpurl']=$durl;
        }
    }
    $GLOBALS['returncookie']=$cookie=getcookies($r,$cookie);
    preg_match_all("/(^.*)\r\n\r\n(.+?)$/Uis", $r, $nn);
    $r = chunk($nn[2][0]);
    return $r;
}

function getCookies($nr,&$cookie=""){
    preg_match_all("/Set-Cookie\:\s+(.*)(?:$|(?:path|domain|expires)\s*=)/Uimx",$nr,$nn);
    if(count($nn[1])==0) return;
    $cstr="$cookie";
    for($i=0;$i<count($nn[1]);$i++){
        $cstr.=";".trim($nn[1][$i]);
    }
    $str=preg_replace("/\;+/",";",$cstr);
    $str=preg_replace("/^\;/","",$str);
    $nn=explode(";",$str);
    $arr=array();
    $arr2=array();
    foreach($nn as $k=>$v){
        $ar=explode("=",$v);
        if(trim($ar[0])!=""){
            $arr2[trim($ar[0])]=trim($ar[1]);
        }
    }
    $s="";
    foreach($arr2 as $k=>$v){
        $s.=$k."=".$v.";";
    }
    return $s;
}

function chunk($result)
{
    if (@gzinflate(substr($result, 10))) {
        $result = @gzinflate(substr($result, 10));
    }
    if (strpos($result, 'chunk')) {
        $tmpfile = tempnam('/tmp', 'webcache');
        $fp = fopen($tmpfile, 'w');
        fwrite($fp, $result);
        fclose($fp);
        ob_start();
        readgzfile($tmpfile);
        $result = ob_get_contents();
        ob_end_clean();
        unlink($tmpfile);
    }
    return $result;
}

function getProxy(){
	$fn=fopen("d:/collect_new/proxyhtml.txt","r");$nr=fread($fn,filesize("d:/collect_new/proxyhtml.txt"));fclose($fn); 
	preg_match_all("/([0-9\:\.]{8,}(\s*[0-9]+)?)/",$nr,$nn0);
	for($t=0;$t<count($nn0[1]);$t++){
		$proxy=preg_replace("/\s/",":",trim($nn0[1][$t]));
		//echo $proxy."\r\n";
	}
}
?>